package cn.txun.springboot61.controller;


import cn.txun.springboot61.entity.Comment;
import cn.txun.springboot61.entity.User;
import cn.txun.springboot61.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @RequestMapping("/comment/insert")
    public int insert(Comment comment, HttpSession session){
        //从session取出User对象
        User u = (User)session.getAttribute("u");
        if(u==null){//用户没有登录
            return 2;
        }
        //将author和created封装到Comment对象中
        comment.setAuthor(u.getNickname());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        return 1;//评论成功
    }

    @RequestMapping("/comment/select")
    public List<Comment> select(int id){
        return commentMapper.selectByWeiboId(id);
    }

}
